//CSCE351 Project 1
//Amy Holdcroft
//Alex Baruth
//Curtis Richards
#include <stdio.h>
#include "sys/alt_timestamp.h"
#include "sys/alt_alarm.h"
#include "alt_types.h"
//Defines how long the interrupt will last, defined by QUANTUM_LENGTH
#define ALARMTICKS(x) ((alt_ticks_per_second()*(x))/10)

void prototype_os();
alt_u32 myInterruptHandler(void * context);

void prototype_os() {
	// MAX value of 100000, to give sufficient amount of delay for output.  
	int MAX = 100000;
	// Here: initialize the timer and its interrupt handler
	// Initializing the alarm
	static alt_alarm interrupt;
	// Interrupt handler: myInterruptHandler
	alt_alarm_start(&interrupt, alt_ticks_per_second(), myInterruptHandler,
			NULL);
	while (1) {
		// assembly nop instruction, does nothing. 
		asm("nop");
		alt_printf(
				"This is the prototype os for my exciting CSE351 course projects!\n");
		// Here: think about what MAX is used for. Pick up an appropriate value for it experimentally.
		//Loop to delay output
		int j = 0;
		for (j = 0; j < MAX; j++)
			;
	}
}
//Interrupt Handler
alt_u32 myInterruptHandler(void * context) {
	//After testing, 10 gave the best results
	int QUANTUM_LENGTH = 10;
	// Added newlines for clarity of the output.
	alt_printf("Interrupted by the timer!\n\n\n");

	return ALARMTICKS(QUANTUM_LENGTH);
}

void main() {
	//Here: call prototype_os
	prototype_os();
}
